package fgl.common.config;

/**
 * @author fuguangli
 * @description 前沿类
 * @Create date:    2017/7/3
 */

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

@Data
@Configuration
@ConfigurationProperties(prefix = "db")
@PropertySource("classpath:db.properties")
public class DataSourceConfig {
    private String driver;
    private String url;
    private String username;
    private String password;
    private Integer maxPoolSize;
    private Integer minPoolSize;
    private Integer initialPoolSize;
    private Long maxWait;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxActive(maxPoolSize);
        dataSource.setMaxWait(initialPoolSize);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);

        dataSource.setMaxWait(maxWait);

//        dataSource.setRemoveAbandoned(true);
//        dataSource.setRemoveAbandonedTimeout(180);
//        dataSource.setLogAbandoned(true);

        return dataSource;
    }
}
